//
//  NewHomeSliderView.m
//  NewPro
//
//  Created by RYX on 2019/4/23.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "NewHomeSliderView.h"
#import "TYCyclePagerView.h"
#import "TYCyclePagerViewCell.h"
#import "HomeBanner2.h"
#import "ActivityDetailController.h"
#import "HomeList.h"

@interface NewHomeSliderView ()<TYCyclePagerViewDelegate,TYCyclePagerViewDataSource>

@property (nonatomic,strong) TYCyclePagerView *pagerView;
@property (nonatomic, copy) NSArray *datas;

@end

@implementation NewHomeSliderView

- (TYCyclePagerView *)pagerView{
    
    if (!_pagerView) {
        
        _pagerView = [[TYCyclePagerView alloc] init];
        _pagerView.isInfiniteLoop = YES;
        _pagerView.autoScrollInterval = 3.0;
        _pagerView.dataSource = self;
        _pagerView.delegate = self;
        [_pagerView registerClass:[TYCyclePagerViewCell class] forCellWithReuseIdentifier:@"cellId"];
    }
    return _pagerView;
}

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = UIColor.whiteColor;
        [self setUI];
    }
    return self;
}

- (void)setUI{

    [self addSubview:self.pagerView];
    
    [self.pagerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.equalTo(self);
        make.top.equalTo(self).offset(15);
        make.bottom.equalTo(self).offset(-15);
    }];
}

#pragma mark - TYCyclePagerViewDataSource
- (NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView {
    
    return _datas.count;
}

- (UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index {
    
    HomeBanner2 *model = _datas[index];
    
    TYCyclePagerViewCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndex:index];
    cell.layer.cornerRadius = 5;
    [cell.im sd_setImageWithURL:[NSURL URLWithString:model.imgBanner]];
    return cell;
}

- (TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView {
    
    TYCyclePagerViewLayout *layout = [[TYCyclePagerViewLayout alloc]init];
    layout.itemSize = CGSizeMake(CGRectGetWidth(pageView.frame)*0.9, CGRectGetHeight(pageView.frame));
    layout.itemSpacing = 15;
    layout.itemHorizontalCenter = YES;
    return layout;
}

- (void)pagerView:(TYCyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    
//    NSLog(@"%ld ->  %ld",fromIndex,toIndex);
}

- (void)pagerView:(TYCyclePagerView *)pageView didSelectedItemCell:(__kindof UICollectionViewCell *)cell atIndex:(NSInteger)index{
    
    HomeBanner2 *model = _datas[index];
    if (model.imgPath.length <=0) {
        
        return;
    }
    
    if (![[HomeList sharedInstance].hadRealName isEqualToString:@"4"]) {
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(judgeIfHadRealName)]) {
            
            [self.delegate judgeIfHadRealName];
            return;
        }
        return;
    }
    
    ActivityDetailController *VC = [[ActivityDetailController alloc]init];
    VC.imagepath = model.imgPath;
    [VC setHidesBottomBarWhenPushed:YES];
    [[self getCurrentViewController].navigationController pushViewController:VC animated:YES];
}

- (void)setListArr:(NSArray *)listArr{
    
    NSMutableArray *arr1 = [[NSMutableArray alloc]init];
    for (NSInteger i = 0; i < listArr.count; i++) {
        
        NSArray *subarr = listArr[i];
        [arr1 addObject:subarr[0]];
        
    }
    NSArray *arr2 = [HomeBanner2 mj_objectArrayWithKeyValuesArray:arr1];
    
    NSMutableArray *arr3 = [NSMutableArray array];
    for (HomeBanner2 *model in arr2) {
        
        if (model.imgBanner.length > 0) {
            
            [arr3 addObject:model];
        }
    }
    
    _datas = arr3;
    [self.pagerView reloadData];
}
@end
